package com.onlineclass.dao;

import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.demo.util.JDBCUtils;

public class SMSCodeDAO {

	QueryRunner qr = new QueryRunner();
	
	/**
	 * 保存验证码
	 * @param code
	 * @param mobile
	 */
	public void save(Integer code, String mobile) {
		try {
			// 先删这个手机号的之前的验证码，再发送新的。这样的话，这张表的手机号，只有一个验证码
			qr.update(JDBCUtils.getConnection(), 
					"delete from t_smscode where mobile=?", 
					mobile);
			qr.update(JDBCUtils.getConnection(), 
					"insert into t_smscode(code,mobile,created) values(?,?,now())", 
					code, mobile);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public long verify(Integer code, String mobile) {
		long nums = 0;
		try {
			nums = qr.query(JDBCUtils.getConnection(), 
					"select count(1) from t_smscode where code=? and mobile=?", 
					new ScalarHandler<>(),
					code, mobile);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return nums;
	}
}
